<?	if ($c = stack('name')) { $_id_component = $DATA['components'][$c]['id'];			//	cpn id value

$temp = array();


$_table = 'data, xdata';
$_crit =  "(data.id=xdata.id) AND (data.name IN ('body', 'anons'))";
$_per_page = 20;


$d = search_safe(trim(getdata('q')));
$length_d = mb_strlen($d);  

if ( $length_d < 3 ) {
	diags("<h3>Your search did not match any documents.</h3><br /><strong>Suggestions:</strong><br />Make sure all words are spelled correctly?<br />Try different keywords.<br />Try more general keywords.");	
	return true;

} else {
	//$_crit2 = " AND xdata.value LIKE '%$d%'";
	//$_crit2 = " AND REGEXP_LIKE(xdata.value, '$d', 'i')";
	$_crit2 = " AND CONTAINS(xdata.filtered, '%$d%', 1) > 0";
}

/***** paging *****/

//dbdebug();

if ($num = datafetch('db_item', tb($_table), $_crit.$_crit2, "COUNT(*) as num")) {

	if ($num = (int) $num['num']) {

		$temp['paging'] = createPaging(array(
			'num' => $num, 
			'page' => param('page'), 
			'perPage' => $_per_page,
			'pagesBefore' => 4, 'pagesAfter' => 4,
			'pagesAtStart' => 4, 'pagesAtEnd' => 4,
		));

		$found = datafetch_array('db_list', array('table' => tb($_table),
						'where' => $_crit.$_crit2,
						'select' => 'xdata.value value, data.id id, 
										data.id_parent id_parent, data.assoc assoc,
										data.name name, data.id_data id_data',
						'index' => 'id_parent',
						'limit' => $temp['paging']['limit']));
															  
															  
		// separate found blocks by kind
		$bodies = array('text' => array(), 'article' => array());
		foreach($found as $id => $v)
			switch($v['id_data']) {
				case 16: $bodies['article'][$id] = $v;	break;
				case 15: $bodies['text'][$id] = $v;	break;
			}

		$links=array();
															  
// ASSEMBLY
function assembly_content ($content, $d, $length_d ) {

	$content = preg_replace('/[\s\n]+/', ' ', strip_tags($content));
	$new_d = mb_substr($content, $pos_d = mb_stripos($content, $d), $length_d);
	
	$r = explode($new_d, $content, 2);

	if (mb_strlen($r[0]) > 50) $r[0] = mb_substr($r[0], -50);
	if (mb_strlen($r[1]) > 150) $r[1] = mb_substr($r[1], 0, 150);

	return array(
		'word' => $new_d,
		'text_start' => $r[0],
		'text_end' => $r[1],
	);
}


// ARTICLES

	if (sizeof($bodies['article'])) {

		$keys = join(',', array_keys($bodies['article']));
															  
		$t = datafetch_array('db_list', array('table' => tb('data'),	// get titles
			  'where' => "id_parent IN ($keys) AND name='title'"));

		foreach($t as $k => $v)
			if ($bodies['article'][$v['id_parent']]['assoc'] == $v['assoc'])						//compare lang
				$bodies['article'][$v['id_parent']]['title'] = $v['value'];				 
		
	
		/***** sections *****/													
		$_crit3 = "data.id IN ($keys)";					//titles output 	
		$title_ids = datafetch_array('db_list', array('table' => tb('data'),
															  'where' => $_crit3,
															  'index' => 'id'));

		$components = array(); foreach($title_ids as $v) $components[] = $v['id_parent'];
		
		$_crit4 = "data.id IN (".join(', ', $components).")";											//section output													
		$tsection_ids = datafetch_array('db_list', array('table' => tb('data'),
															  'where' => $_crit4,
															  'index' => 'id'));

		$titlids = $bodies['article'];

		$res = array();

		foreach( $titlids as $tid => $titlid ) {
			
			$article_id = $titlid['id_parent'];

			$title = $titlid['title'];
	
			$component_id = $title_ids[$article_id]['id_parent'];
			
			if (!($tsection_ids[$component_id]['attr'] & COM_ACTIVE)) continue;

			$alias = $CURRENT->getAliasById($tsection_ids[$component_id]['id_parent']);

//debug(array($article_id, $component_id, $tsection_ids[$component_id]['id_parent'], $alias));			
			
			$url = '';
			if ($titlid['assoc'] && ($titlid['assoc'] != LANG_DEF)) 
				$url = '/'.$titlid['assoc'];
			$url .= "$alias$article_id/";
			
			$res[$tid] = array_merge($titlids[$tid], assembly_content($titlid['value'], $d, $length_d),  array(
				'title' => $title ? $title : '---',
				'url' => $url,
			));
		}

//debug($titlids);

		$bodies['article'] = $res;
	}
// END OF ARTICLES





// TEXT

//debug($bodies['text'], 'text');

	if (sizeof($bodies['text'])) {
		
		$res = array();

		$cpns = datafetch_array('db_list', array('table' => tb('data'), 'index' => 'id',
				  'where' => "id IN (".join(',', array_keys($bodies['text'])).")"));
		
		foreach($bodies['text'] as $tid => $tdata) {
			
			$t = $CURRENT->getPageById($cpns[$tdata['id_parent']]['id_parent']);
			$title = $t['localname'];
			$alias_text = $CURRENT->getAliasById($cpns[$tdata['id_parent']]['id_parent']);

			$url = '';
			if ($tdata['assoc'] && ($tdata['assoc'] != LANG_DEF)) 
				$url = '/'.$tdata['assoc'];
			$url .= "$alias_text";
			//$url .= "$alias_text$article_id/";
			
			$res[$tid] = array_merge($bodies['text'][$tid], assembly_content($tdata['value'], $d, $length_d), array(
				'title' => $title ? $title : '---',
				'url' => $url,
			));
	
		}
		
		$bodies['text'] = $res;
	}
	
// END OF TEXT
		
		$links = array();
		
		foreach($bodies as $v) $links = array_merge($links, $v);

		//debug($found);
		//dbdebug();	
		
	} // else zero matches found
} // else request failed

$temp['links'] = $links;

$DATA['components'][$c]['data'] = $temp;

unstack();
}
